const vscode = require('vscode');
const fs = require('fs');
const { ConfigName } = require('./Constant')
const getConfig = async () => {
    let configData
    //处理cjs
    const cjsFiles = await vscode.workspace.findFiles(`${ConfigName}.cjs`, '**​/node_modules/**', 1)
    if (cjsFiles.length > 0) {
        const data = await vscode.workspace.fs.readFile(cjsFiles[0])
        eval(data.toString())
        configData = module.exports
    }
    if (!configData) {
        // 处理js文件
        const [jsFiles] = await Promise.all([
            vscode.workspace.findFiles(`${ConfigName}.js`, '**​/node_modules/**', 1),
        ])
        const allJsFiles = [...jsFiles]
        if (allJsFiles.length > 0) {
            configData = require(allJsFiles[0].fsPath)
        }
    }

    if (!configData) {
        //处理JSON
        const jsonFiles = await vscode.workspace.findFiles(`${ConfigName}.cjs`, '**​/node_modules/**', 1)
        if (jsonFiles.length > 0) {
            const data = await vscode.workspace.fs.readFile(jsonFiles[0])
            console.log(data.toString())
            eval(data.toString())
            console.log(module.exports)
            configData = JSON.parse(data.toString())
        }
    }
    return configData
}

module.exports = getConfig;